<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: specials_price.php 11 2013-09-21 11:43:11Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 11 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-21 11:43:11 +0000 (Sat, 21 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

defined('_VALID_XTC') or die('Direct Access to this location is not allowed.');
require_once(DIR_FS_INC.'inc.get_tax_rate.php');

$groups = get_customers_statuses();
foreach($groups as $group) {
	if(!empty($products_data['products_specials_price_'.$group['id']]) && !isset($products_data['del_special'])) {
		$post_price = $products_data['products_specials_price_'.$group['id']];
		
		$post_price = str_replace(',', '.', $post_price);
			
		if(substr($post_price, -1) == '%') {
			$special_price_raw = substr($post_price, 0, -1);
			$post_price = ($post_price - (($special_price_raw / 100) * $special_price_raw));
		}
		
		if(PRICE_IS_BRUTTO == 'true')		
			$special_price = round(($post_price / (get_tax_rate($products_data['products_tax_class_id']) + 100) * 100), PRICE_PRECISION);
		else
			$special_price = $post_price;
		
		$expires_date = '';
		if($products_data['expires_date'] !='')
			$expires_date = $products_data['expires_date'].' 00:00:00';

		if($group['id'] == '0') {
			$checkSpecial = $this->db->db_query("SELECT specials_price_".$group['id']." FROM ".TABLE_SPECIALS." WHERE products_id = '".$products_data['products_id']."' ");
			
			if(!empty($checkSpecial->fields['specials_price_'.$group['id']])) {
				$this->db->db_query("UPDATE 
										".TABLE_SPECIALS." 
									SET 
										specials_price_".$group['id']." = '".$special_price."',
										specials_quantity = '".$products_data['specials_quantity']."',
										specials_last_modified = NOW(),
										expires_date = '".$expires_date."',
										status = '".(int)$_POST['specials_status']."'
									WHERE
										products_id = '".$products_id."' ");

			} elseif(empty($checkSpecial->fields['specials_price_'.$group['id']])) {
				$this->db->db_query("INSERT INTO ".TABLE_SPECIALS."
										(products_id, 
										specials_quantity, 
										specials_price_".$group['id'].",
										specials_date_added,
										expires_date,
										status)
									VALUES
										('".$products_id."',
										'".$products_data['specials_quantity']."',
										'".$special_price."',
										NOW(),
										'".$expires_date."',
										1)");
			}
		} elseif(!empty($post_price))
			$this->db->db_query("UPDATE ".TABLE_SPECIALS." SET specials_price_".$group['id']." = '".$special_price."' WHERE products_id = '".$products_id."'");

	} elseif((empty($products_data['products_specials_price_'.$group['id']]) OR $products_data['products_specials_price_'.$group['id']] == '0') && !isset($products_data['del_special']))
		$this->db->db_query("UPDATE ".TABLE_SPECIALS." SET specials_price_".$group['id']." = '' WHERE products_id = '".$products_id."'");
}

if(isset($products_data['del_special']))
	$this->db->db_query("DROP FROM ".TABLE_SPECIALS." WHERE products_id = '".$products_id."' ");